<!DOCTYPE html><html class=split lang=en-US-x-hixie><script src=../link-fixup.js defer=""></script>
<!-- Mirrored from html.spec.whatwg.org/dev/document-lifecycle.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 10 Sep 2025 08:36:28 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<meta charset=utf-8><meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name=viewport><title>HTML Standard, Edition for Web Developers</title><meta content=#3c790a name=theme-color><meta content="light dark" name=color-scheme><link rel=stylesheet href=../../resources.whatwg.org/standard-shared-with-dev.css crossorigin=""><link rel=icon href=https://resources.whatwg.org/logo.svg crossorigin=""><link rel=stylesheet href=styles.css crossorigin=""><script>
   function toggleStatus(div) {
     div.parentNode.classList.toggle('wrapped');
   }
   function setLinkFragment(link) {
     link.hash = location.hash;
   }
  </script><body>
  <script async="" src=search.js></script>
  
  
  <header id=head class="head with-buttons">
   <a href=https://whatwg.org/ class=logo><img width=100 alt=WHATWG crossorigin="" class=darkmode-aware src=https://resources.whatwg.org/logo.svg height=100></a>
   
   <hgroup><h1><a rel=home href=index.html>HTML: The Living Standard</a></h1><p id=dev-edition-h2>Edition for Web Developers — Last Updated <span class=pubdate>10 September 2025</span></hgroup>
   

   <div id=search>
    <input placeholder="Search. Press '/'" autocomplete=off name=query id=query type=search>
    <ol id=results></ol>
   </div>
  </header>

  

  

  

  
  

  
  

  

  <nav><a href=browsing-the-web.html>← 7.4 Navigation and session
  history</a> — <a href=index.html>Table of Contents</a> — <a href=speculative-loading.html>7.6 Speculative loading →</a></nav><ol class=toc><li><ol><li><a href=document-lifecycle.html#document-lifecycle><span class=secno>7.5</span> Document lifecycle</a><ol><li><a href=document-lifecycle.html#shared-document-creation-infrastructure><span class=secno>7.5.1</span> Shared document creation infrastructure</a><li><a href=document-lifecycle.html#read-html><span class=secno>7.5.2</span> Loading HTML documents</a><li><a href=document-lifecycle.html#read-xml><span class=secno>7.5.3</span> Loading XML documents</a><li><a href=document-lifecycle.html#read-text><span class=secno>7.5.4</span> Loading text documents</a><li><a href=document-lifecycle.html#read-multipart-x-mixed-replace><span class=secno>7.5.5</span> Loading <code>multipart/x-mixed-replace</code>
  documents</a><li><a href=document-lifecycle.html#read-media><span class=secno>7.5.6</span> Loading media documents</a><li><a href=document-lifecycle.html#read-ua-inline><span class=secno>7.5.7</span> Loading a document for inline content that doesn't have a
  DOM</a><li><a href=document-lifecycle.html#loading-documents><span class=secno>7.5.8</span> Finishing the loading process</a><li><a href=document-lifecycle.html#unloading-documents><span class=secno>7.5.9</span> Unloading documents</a><li><a href=document-lifecycle.html#destroying-documents><span class=secno>7.5.10</span> Destroying documents</a><li><a href=document-lifecycle.html#aborting-a-document-load><span class=secno>7.5.11</span> Aborting a document load</a></ol></ol></ol><h3 id=document-lifecycle><span class=secno>7.5</span> Document lifecycle<a href=#document-lifecycle class=self-link></a></h3>

  <h4 id=shared-document-creation-infrastructure><span class=secno>7.5.1</span> Shared document creation infrastructure<a href=#shared-document-creation-infrastructure class=self-link></a></h4>

  <p>When loading a document using one of the below algorithms, we use the
  following steps to <dfn id=initialise-the-document-object>create and initialize a <code>Document</code> object</dfn>,
  given a <a href=https://dom.spec.whatwg.org/#concept-document-type id=shared-document-creation-infrastructure:concept-document-type data-x-internal=concept-document-type>type</a> <var>type</var>, <a href=https://dom.spec.whatwg.org/#concept-document-content-type id=shared-document-creation-infrastructure:concept-document-content-type data-x-internal=concept-document-content-type>content type</a> <var>contentType</var>, and
  <a id=shared-document-creation-infrastructure:navigation-params href=browsing-the-web.html#navigation-params>navigation params</a> <var>navigationParams</var>:</p>

  <p class=note><code>Document</code> objects are also created when <a id=shared-document-creation-infrastructure:creating-a-new-browsing-context href=document-sequences.html#creating-a-new-browsing-context>creating a new browsing
  context and document</a>; such <a href=dom.html#is-initial-about:blank id=shared-document-creation-infrastructure:is-initial-about:blank>initial
  <code>about:blank</code></a> <code>Document</code> are never created by this algorithm. Also,
  <a href=document-sequences.html#concept-document-bc id=shared-document-creation-infrastructure:concept-document-bc>browsing context</a>-less <code>Document</code> objects can
  be created via various APIs, such as <code id=shared-document-creation-infrastructure:dom-domimplementation-createhtmldocument><a data-x-internal=dom-domimplementation-createhtmldocument href=https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument>document.implementation.createHTMLDocument()</a></code>.</p>

  <ol><li>
    <p>Let <var>browsingContext</var> be the result of <span>obtaining a browsing context to use for a
    navigation response</span> given <var>navigationParams</var>.</p>

    <p class=note>This can result in a <a href=#browsing-context-group-switches-due-to-cross-origin-opener-policy>browsing context
    group switch</a>, in which case <var>browsingContext</var> will be a <a href=document-sequences.html#creating-a-new-browsing-context id=shared-document-creation-infrastructure:creating-a-new-browsing-context-2>newly-created</a> <a id=shared-document-creation-infrastructure:browsing-context href=document-sequences.html#browsing-context>browsing context</a> instead
    of being <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-navigable id=shared-document-creation-infrastructure:navigation-params-navigable>navigable</a>'s <a href=document-sequences.html#nav-bc id=shared-document-creation-infrastructure:nav-bc>active browsing
    context</a>. In such a case, the created <code>Window</code>, <code>Document</code>, and
    <a id=shared-document-creation-infrastructure:agent href=https://tc39.es/ecma262/#sec-agents data-x-internal=agent>agent</a> will not end up being used; because the created <code>Document</code>'s
    <a href=https://dom.spec.whatwg.org/#concept-document-origin id=shared-document-creation-infrastructure:concept-document-origin data-x-internal=concept-document-origin>origin</a> is <a href=browsers.html#concept-origin-opaque id=shared-document-creation-infrastructure:concept-origin-opaque>opaque</a>, we will end up creating a new <a id=shared-document-creation-infrastructure:agent-2 href=https://tc39.es/ecma262/#sec-agents data-x-internal=agent>agent</a>
    and <code>Window</code> <a href=#create-new-agent-and-window>later in this algorithm</a> to
    go along with the new <code>Document</code>.</p>
   <li>
    <p>Let <var>permissionsPolicy</var> be the result of <a id=shared-document-creation-infrastructure:creating-a-permissions-policy-from-a-response href=https://w3c.github.io/webappsec-feature-policy/#create-from-response data-x-internal=creating-a-permissions-policy-from-a-response>creating a permissions policy from a
    response</a> given <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-navigable id=shared-document-creation-infrastructure:navigation-params-navigable-2>navigable</a>'s <a href=document-sequences.html#nav-container id=shared-document-creation-infrastructure:nav-container>container</a>, <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-origin id=shared-document-creation-infrastructure:navigation-params-origin>origin</a>, and <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response>response</a>. <a href=references.html#refsPERMISSIONSPOLICY>[PERMISSIONSPOLICY]</a></p>

    <div class=note>
     <p>The <a id=shared-document-creation-infrastructure:creating-a-permissions-policy-from-a-response-2 href=https://w3c.github.io/webappsec-feature-policy/#create-from-response data-x-internal=creating-a-permissions-policy-from-a-response>creating a permissions policy from a response</a> algorithm makes use of the
     passed <a id=shared-document-creation-infrastructure:concept-origin href=browsers.html#concept-origin>origin</a>. If <code id=shared-document-creation-infrastructure:dom-document-domain><a href=browsers.html#dom-document-domain>document.domain</a></code> has
     been used for <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-navigable id=shared-document-creation-infrastructure:navigation-params-navigable-3>navigable</a>'s <a href=document-sequences.html#nav-container-document id=shared-document-creation-infrastructure:nav-container-document>container document</a>, then its <a href=https://dom.spec.whatwg.org/#concept-document-origin id=shared-document-creation-infrastructure:concept-document-origin-2 data-x-internal=concept-document-origin>origin</a> cannot be <a id=shared-document-creation-infrastructure:same-origin-domain href=browsers.html#same-origin-domain>same origin-domain</a> with
     the passed origin, because these steps run before the <var>document</var> is created, so it
     cannot itself yet have used <code id=shared-document-creation-infrastructure:dom-document-domain-2><a href=browsers.html#dom-document-domain>document.domain</a></code>. Note
     that this means that Permissions Policy checks are less permissive compared to doing a
     <a id=shared-document-creation-infrastructure:same-origin href=browsers.html#same-origin>same origin</a> check instead.</p>

     <p>See below for some examples of this in action.</p>
    </div>
   <li><p>Let <var>creationURL</var> be <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-2>response</a>'s <a href=https://fetch.spec.whatwg.org/#concept-response-url id=shared-document-creation-infrastructure:concept-response-url data-x-internal=concept-response-url>URL</a>.<li><p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=shared-document-creation-infrastructure:navigation-params-request>request</a>
   is non-null, then set <var>creationURL</var> to <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=shared-document-creation-infrastructure:navigation-params-request-2>request</a>'s <a href=https://fetch.spec.whatwg.org/#concept-request-current-url id=shared-document-creation-infrastructure:concept-request-current-url data-x-internal=concept-request-current-url>current URL</a>.<li><p>Let <var>window</var> be null.<li>
    <p>If <var>browsingContext</var>'s <a id=shared-document-creation-infrastructure:active-document href=document-sequences.html#active-document>active document</a>'s <a id=shared-document-creation-infrastructure:is-initial-about:blank-2 href=dom.html#is-initial-about:blank>is initial
    <code>about:blank</code></a> is true, and <var>browsingContext</var>'s <a id=shared-document-creation-infrastructure:active-document-2 href=document-sequences.html#active-document>active
    document</a>'s <a href=https://dom.spec.whatwg.org/#concept-document-origin id=shared-document-creation-infrastructure:concept-document-origin-3 data-x-internal=concept-document-origin>origin</a> is <a id=shared-document-creation-infrastructure:same-origin-domain-2 href=browsers.html#same-origin-domain>same
    origin-domain</a> with <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-origin id=shared-document-creation-infrastructure:navigation-params-origin-2>origin</a>, then set <var>window</var> to
    <var>browsingContext</var>'s <a id=shared-document-creation-infrastructure:active-window href=document-sequences.html#active-window>active window</a>.</p>

    <p class=note>This means that both the <a href=dom.html#is-initial-about:blank id=shared-document-creation-infrastructure:is-initial-about:blank-3>initial
    <code>about:blank</code></a> <code>Document</code>, and the new <code>Document</code> that
    is about to be created, will share the same <code>Window</code> object.</p>
  <li id=create-new-agent-and-window>
    <p>Otherwise:</p>

    <ol><li><p>Let <var>oacHeader</var> be the result of <a id=shared-document-creation-infrastructure:getting-a-structured-field-value href=https://fetch.spec.whatwg.org/#concept-header-list-get-structured-header data-x-internal=getting-a-structured-field-value>getting a structured field value</a>
     given `<code id=shared-document-creation-infrastructure:origin-agent-cluster><a href=browsers.html#origin-agent-cluster>Origin-Agent-Cluster</a></code>` and "<code>item</code>" from
     <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-3>response</a>'s
     <a href=https://fetch.spec.whatwg.org/#concept-response-header-list id=shared-document-creation-infrastructure:concept-response-header-list data-x-internal=concept-response-header-list>header list</a>.<li><p>Let <var>requestsOAC</var> be true if <var>oacHeader</var> is not
     null and <var>oacHeader</var>[0] is the <a href=https://httpwg.org/specs/rfc8941.html#boolean id=shared-document-creation-infrastructure:http-structured-header-boolean data-x-internal=http-structured-header-boolean>boolean</a> true; otherwise false.<li><p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-reserved-environment id=shared-document-creation-infrastructure:navigation-params-reserved-environment>reserved environment</a> is a
     <span>non-secure context</span>, then set <var>requestsOAC</var> to false.<li><p>Let <var>agent</var> be the result of <span>obtaining a similar-origin window agent</span>
     given <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-origin id=shared-document-creation-infrastructure:navigation-params-origin-3>origin</a>,
     <var>browsingContext</var>'s <a href=document-sequences.html#tlbc-group id=shared-document-creation-infrastructure:tlbc-group>group</a>, and
     <var>requestsOAC</var>.<li>
      <p>Let <var>realmExecutionContext</var> be the result of <span>creating a new realm</span>
      given <var>agent</var> and the following customizations:</p>

      <ul><li><p>For the global object, create a new <code>Window</code> object.<li><p>For the global <b>this</b> binding, use <var>browsingContext</var>'s
       <code id=shared-document-creation-infrastructure:windowproxy><a href=nav-history-apis.html#windowproxy>WindowProxy</a></code> object.</ul>
     <li><p>Set <var>window</var> to the <span>global
     object</span> of <var>realmExecutionContext</var>'s Realm component.<li><p>Let <var>topLevelCreationURL</var> be <var>creationURL</var>.<li><p>Let <var>topLevelOrigin</var> be <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-origin id=shared-document-creation-infrastructure:navigation-params-origin-4>origin</a>.<li>
      <p>If <var>navigable</var>'s <a href=document-sequences.html#nav-container id=shared-document-creation-infrastructure:nav-container-2>container</a> is not null,
      then:</p>

      <ol><li><p>Let <var>parentEnvironment</var> be <var>navigable</var>'s <a href=document-sequences.html#nav-container id=shared-document-creation-infrastructure:nav-container-3>container</a>'s <span>relevant settings object</span>.<li><p>Set <var>topLevelCreationURL</var> to <var>parentEnvironment</var>'s <span>top-level
       creation URL</span>.<li><p>Set <var>topLevelOrigin</var> to <var>parentEnvironment</var>'s <span>top-level
       origin</span>.</ol>
     <li><p><span>Set up a window environment settings object</span> with <var>creationURL</var>,
     <var>realmExecutionContext</var>, <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-reserved-environment id=shared-document-creation-infrastructure:navigation-params-reserved-environment-2>reserved environment</a>,
     <var>topLevelCreationURL</var>, and <var>topLevelOrigin</var>.</ol>

    <p class=note>This is the usual case, where the new <code>Document</code> we're about to
    create gets a new <code>Window</code> to go along with it.</p>
   <li><p>Let <var>loadTimingInfo</var> be a new <span>document load timing info</span> with its
   <span>navigation start time</span> set to <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-4>response</a>'s <a href=https://fetch.spec.whatwg.org/#concept-response-timing-info id=shared-document-creation-infrastructure:concept-response-timing-info data-x-internal=concept-response-timing-info>timing info</a>'s <a href=https://fetch.spec.whatwg.org/#fetch-timing-info-start-time id=shared-document-creation-infrastructure:fetch-timing-info-start-time data-x-internal=fetch-timing-info-start-time>start time</a>.<li>
    <p>Let <var>document</var> be a new <code>Document</code>, with</p>

    <dl class=props><dt><a href=https://dom.spec.whatwg.org/#concept-document-type id=shared-document-creation-infrastructure:concept-document-type-2 data-x-internal=concept-document-type>type</a><dd><var>type</var><dt><a href=https://dom.spec.whatwg.org/#concept-document-content-type id=shared-document-creation-infrastructure:concept-document-content-type-2 data-x-internal=concept-document-content-type>content type</a><dd><var>contentType</var><dt><a href=https://dom.spec.whatwg.org/#concept-document-origin id=shared-document-creation-infrastructure:concept-document-origin-4 data-x-internal=concept-document-origin>origin</a><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-origin id=shared-document-creation-infrastructure:navigation-params-origin-5>origin</a><dt><a href=document-sequences.html#concept-document-bc id=shared-document-creation-infrastructure:concept-document-bc-2>browsing context</a><dd><var>browsingContext</var>

     <dt><a href=dom.html#concept-document-policy-container id=shared-document-creation-infrastructure:concept-document-policy-container>policy container</a><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-policy-container id=shared-document-creation-infrastructure:navigation-params-policy-container>policy
     container</a><dt><a href=dom.html#concept-document-permissions-policy id=shared-document-creation-infrastructure:concept-document-permissions-policy>permissions policy</a><dd><var>permissionsPolicy</var><dt><span>active sandboxing flag set</span><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-sandboxing id=shared-document-creation-infrastructure:navigation-params-sandboxing>final sandboxing
     flag set</a><dt><a href=dom.html#concept-document-coop id=shared-document-creation-infrastructure:concept-document-coop>opener policy</a><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-coop id=shared-document-creation-infrastructure:navigation-params-coop>cross-origin opener
     policy</a><dt><span>load timing info</span><dd><var>loadTimingInfo</var><dt><span>was created via cross-origin redirects</span><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-5>response</a>'s
     <a href=https://fetch.spec.whatwg.org/#response-has-cross-origin-redirects id=shared-document-creation-infrastructure:concept-response-has-cross-origin-redirects data-x-internal=concept-response-has-cross-origin-redirects>has cross-origin
     redirects</a><dt><a href=dom.html#concept-document-navigation-id id=shared-document-creation-infrastructure:concept-document-navigation-id>during-loading navigation ID for WebDriver BiDi</a><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-id id=shared-document-creation-infrastructure:navigation-params-id>id</a><dt><a href=https://dom.spec.whatwg.org/#concept-document-url id="shared-document-creation-infrastructure:the-document's-address" data-x-internal="the-document's-address">URL</a><dd><var>creationURL</var><dt><span>current document readiness</span><dd>"<code>loading</code>"<dt><a href=dom.html#concept-document-about-base-url id=shared-document-creation-infrastructure:concept-document-about-base-url>about base URL</a><dd><var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-about-base-url id=shared-document-creation-infrastructure:navigation-params-about-base-url>about base URL</a><dt><a href=https://dom.spec.whatwg.org/#concept-document-allow-declarative-shadow-roots id=shared-document-creation-infrastructure:concept-document-allow-declarative-shadow-roots data-x-internal=concept-document-allow-declarative-shadow-roots>allow declarative shadow
     roots</a><dd>true<dt><a href=https://dom.spec.whatwg.org/#document-custom-element-registry id=shared-document-creation-infrastructure:document-custom-element-registry data-x-internal=document-custom-element-registry>custom element registry</a><dd>a new <code>CustomElementRegistry</code> object</dl>
   <li><p>Set <var>window</var>'s <span>associated
   <code>Document</code></span> to <var>document</var>.<li><p><a id=shared-document-creation-infrastructure:run-csp-initialization-for-a-document href=https://w3c.github.io/webappsec-csp/#run-document-csp-initialization data-x-internal=run-csp-initialization-for-a-document>Run CSP initialization for a <code>Document</code></a> given
   <var>document</var>. <a href=references.html#refsCSP>[CSP]</a></p>

   <li>
    <p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=shared-document-creation-infrastructure:navigation-params-request-3>request</a> is
    non-null, then:</p>

    <ol><li><p>Set <var>document</var>'s <a href="dom.html#the-document's-referrer" id="shared-document-creation-infrastructure:the-document's-referrer">referrer</a> to the
     empty string.<li><p>Let <var>referrer</var> be <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=shared-document-creation-infrastructure:navigation-params-request-4>request</a>'s <a href=https://fetch.spec.whatwg.org/#concept-request-referrer id=shared-document-creation-infrastructure:concept-request-referrer data-x-internal=concept-request-referrer>referrer</a>.<li>
      <p>If <var>referrer</var> is a <a id=shared-document-creation-infrastructure:url-record href=https://url.spec.whatwg.org/#concept-url data-x-internal=url-record>URL record</a>, then set <var>document</var>'s <a href="dom.html#the-document's-referrer" id="shared-document-creation-infrastructure:the-document's-referrer-2">referrer</a> to the <a href=https://url.spec.whatwg.org/#concept-url-serializer id=shared-document-creation-infrastructure:concept-url-serializer data-x-internal=concept-url-serializer>serialization</a> of <var>referrer</var>.</p>

      <p class=note>Per <cite>Fetch</cite>, <var>referrer</var> will be either a <a id=shared-document-creation-infrastructure:url-record-2 href=https://url.spec.whatwg.org/#concept-url data-x-internal=url-record>URL
      record</a> or "<code>no-referrer</code>" at this point.</p>
     </ol>
   <li>
    <p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-fetch-controller id=shared-document-creation-infrastructure:navigation-params-fetch-controller>fetch
    controller</a> is not null, then:</p>

    <ol><li><p>Let <var>fullTimingInfo</var> be the result of <a href=https://fetch.spec.whatwg.org/#extract-full-timing-info id=shared-document-creation-infrastructure:extract-full-timing-info data-x-internal=extract-full-timing-info>extracting the full timing info</a> from <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-fetch-controller id=shared-document-creation-infrastructure:navigation-params-fetch-controller-2>fetch controller</a>.<li><p>Let <var>redirectCount</var> be 0 if <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-6>response</a>'s <a href=https://fetch.spec.whatwg.org/#response-has-cross-origin-redirects id=shared-document-creation-infrastructure:concept-response-has-cross-origin-redirects-2 data-x-internal=concept-response-has-cross-origin-redirects>has cross-origin redirects</a> is
     true; otherwise <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=shared-document-creation-infrastructure:navigation-params-request-5>request</a>'s <a href=https://fetch.spec.whatwg.org/#concept-request-redirect-count id=shared-document-creation-infrastructure:concept-request-redirect-count data-x-internal=concept-request-redirect-count>redirect count</a>.<li><p><a id=shared-document-creation-infrastructure:create-the-navigation-timing-entry href=https://w3c.github.io/navigation-timing/#dfn-create-the-navigation-timing-entry data-x-internal=create-the-navigation-timing-entry>Create the navigation timing entry</a> for <var>document</var>, given
     <var>fullTimingInfo</var>, <var>redirectCount</var>, <var>navigationTimingType</var>,
     <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-7>response</a>'s
     <a href=https://fetch.spec.whatwg.org/#response-service-worker-timing-info id=shared-document-creation-infrastructure:concept-response-service-worker-timing-info data-x-internal=concept-response-service-worker-timing-info>service worker timing info</a>,
     and <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-8>response</a>'s
     <a href=https://fetch.spec.whatwg.org/#concept-response-body-info id=shared-document-creation-infrastructure:concept-response-body-info data-x-internal=concept-response-body-info>body info</a>.</ol>
   <li><p><a id=shared-document-creation-infrastructure:create-the-navigation-timing-entry-2 href=https://w3c.github.io/navigation-timing/#dfn-create-the-navigation-timing-entry data-x-internal=create-the-navigation-timing-entry>Create the navigation timing entry</a> for <var>document</var>, with
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-9>response</a>'s <a href=https://fetch.spec.whatwg.org/#concept-response-timing-info id=shared-document-creation-infrastructure:concept-response-timing-info-2 data-x-internal=concept-response-timing-info>timing info</a>, <var>redirectCount</var>,
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-nav-timing-type id=shared-document-creation-infrastructure:navigation-params-nav-timing-type>navigation timing
   type</a>, and <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-10>response</a>'s <a href=https://fetch.spec.whatwg.org/#response-service-worker-timing-info id=shared-document-creation-infrastructure:concept-response-service-worker-timing-info-2 data-x-internal=concept-response-service-worker-timing-info>service worker timing info</a>.<li>
    <p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-11>response</a>
    has a `<code id=shared-document-creation-infrastructure:refresh><a href=speculative-loading.html#refresh>Refresh</a></code>` header, then:</p>

    <ol><li><p>Let <var>value</var> be the <a href=https://infra.spec.whatwg.org/#isomorphic-decode id=shared-document-creation-infrastructure:isomorphic-decode data-x-internal=isomorphic-decode>isomorphic decoding</a>
     of the value of the header.<li><p>Run the <span>shared declarative refresh steps</span> with <var>document</var> and
     <var>value</var>.</ol>

    <p class=XXX>We do not currently have a spec for how to handle multiple `<code id=shared-document-creation-infrastructure:refresh-2><a href=speculative-loading.html#refresh>Refresh</a></code>`
    headers. This is tracked as <a href=https://github.com/whatwg/html/issues/2900>issue #2900</a>.</p>
   <li><p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-commit-early-hints id=shared-document-creation-infrastructure:navigation-params-commit-early-hints>commit early hints</a> is not null, then call
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-commit-early-hints id=shared-document-creation-infrastructure:navigation-params-commit-early-hints-2>commit early
   hints</a> with <var>document</var>.<li><p><span>Process link headers</span> given <var>document</var>,
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-12>response</a>, and
   "<code>pre-media</code>".<li>
    <p>If <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-navigable id=shared-document-creation-infrastructure:navigation-params-navigable-4>navigable</a>
    is a <a id=shared-document-creation-infrastructure:top-level-traversable href=document-sequences.html#top-level-traversable>top-level traversable</a>, then <a id=shared-document-creation-infrastructure:process-the-speculation-rules-header href=speculative-loading.html#process-the-speculation-rules-header>process the `<code>Speculation-Rules</code>`
    header</a> given <var>document</var> and <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=shared-document-creation-infrastructure:navigation-params-response-13>response</a>.</p>

    <p class=note>This is conditional because <a href=speculative-loading.html#step-consider-speculative-loads-top-level-only>speculative loads are only considered for
    top-level traversables</a>, so it would be wasteful to fetch these rules otherwise.</p>
   <li><p><a id=shared-document-creation-infrastructure:potentially-free-deferred-fetch-quota href=https://fetch.spec.whatwg.org/#reserve-deferred-fetch-quota data-x-internal=potentially-free-deferred-fetch-quota>Potentially free deferred fetch quota</a> for <var>document</var>.<li><p>Return <var>document</var>.</ol>

  <div class=example>
   <p>In this example, the child document is not allowed to use <code id=shared-document-creation-infrastructure:paymentrequest><a data-x-internal=paymentrequest href=https://w3c.github.io/payment-request/#dom-paymentrequest>PaymentRequest</a></code>,
   despite being <a id=shared-document-creation-infrastructure:same-origin-domain-3 href=browsers.html#same-origin-domain>same origin-domain</a> at the time the child document tries to use
   it. At the time the child document is initialized, only the parent document has set <code id=shared-document-creation-infrastructure:dom-document-domain-3><a href=browsers.html#dom-document-domain>document.domain</a></code>, and the child document has not.</p>

   <pre><code class='html'><c- c>&lt;!-- https://foo.example.com/a.html --&gt;</c->
<c- cp>&lt;!doctype html&gt;</c->
<c- p>&lt;</c-><c- f>script</c-><c- p>&gt;</c->
document<c- p>.</c->domain <c- o>=</c-> <c- t>&apos;example.com&apos;</c-><c- p>;</c->
<c- p>&lt;/</c-><c- f>script</c-><c- p>&gt;</c->
<c- p>&lt;</c-><c- f>iframe</c-> <c- e>src</c-><c- o>=</c-><c- s>b.html</c-><c- p>&gt;&lt;/</c-><c- f>iframe</c-><c- p>&gt;</c-></code></pre>

  <pre><code class='html'><c- c>&lt;!-- https://bar.example.com/b.html --&gt;</c->
<c- cp>&lt;!doctype html&gt;</c->
<c- p>&lt;</c-><c- f>script</c-><c- p>&gt;</c->
document<c- p>.</c->domain <c- o>=</c-> <c- t>&apos;example.com&apos;</c-><c- p>;</c-> <c- c1>// This happens after the document is initialized</c->
<c- k>new</c-> PaymentRequest<c- p>(</c->…<c- p>);</c-> <c- c1>// Not allowed to use</c->
<c- p>&lt;/</c-><c- f>script</c-><c- p>&gt;</c-></code></pre>
  </div>

  <div class=example>
   <p>In this example, the child document <em>is</em> allowed to use
   <code id=shared-document-creation-infrastructure:paymentrequest-2><a data-x-internal=paymentrequest href=https://w3c.github.io/payment-request/#dom-paymentrequest>PaymentRequest</a></code>, despite not being <a id=shared-document-creation-infrastructure:same-origin-domain-4 href=browsers.html#same-origin-domain>same origin-domain</a> at the time
   the child document tries to use it. At the time the child document is initialized, none of
   the documents have set <code id=shared-document-creation-infrastructure:dom-document-domain-4><a href=browsers.html#dom-document-domain>document.domain</a></code> yet so
   <a id=shared-document-creation-infrastructure:same-origin-domain-5 href=browsers.html#same-origin-domain>same origin-domain</a> falls back to a normal <a id=shared-document-creation-infrastructure:same-origin-2 href=browsers.html#same-origin>same origin</a> check.</p>

   <pre><code class='html'><c- c>&lt;!-- https://example.com/a.html --&gt;</c->
<c- cp>&lt;!doctype html&gt;</c->
<c- p>&lt;</c-><c- f>iframe</c-> <c- e>src</c-><c- o>=</c-><c- s>b.html</c-><c- p>&gt;&lt;/</c-><c- f>iframe</c-><c- p>&gt;</c->
<c- c>&lt;!-- The child document is now initialized, before the script below is run. --&gt;</c->
<c- p>&lt;</c-><c- f>script</c-><c- p>&gt;</c->
document<c- p>.</c->domain <c- o>=</c-> <c- t>&apos;example.com&apos;</c-><c- p>;</c->
<c- p>&lt;/</c-><c- f>script</c-><c- p>&gt;</c-></code></pre>

   <pre><code class='html'><c- c>&lt;!-- https://example.com/b.html --&gt;</c->
<c- cp>&lt;!doctype html&gt;</c->
<c- p>&lt;</c-><c- f>script</c-><c- p>&gt;</c->
<c- k>new</c-> PaymentRequest<c- p>(</c->…<c- p>);</c-> <c- c1>// Allowed to use</c->
<c- p>&lt;/</c-><c- f>script</c-><c- p>&gt;</c-></code></pre>
  </div>

  <p>To <dfn id=populate-with-html/head/body>populate with <code>html</code>/<code>head</code>/<code>body</code></dfn> given a
  <code>Document</code> <var>document</var>:</p>

  <ol><li><p>Let <var>html</var> be the result of <a href=https://dom.spec.whatwg.org/#concept-create-element id=shared-document-creation-infrastructure:create-an-element data-x-internal=create-an-element>creating an
   element</a> given <var>document</var>, "<code>html</code>", and the <a id=shared-document-creation-infrastructure:html-namespace-2 href=https://infra.spec.whatwg.org/#html-namespace data-x-internal=html-namespace-2>HTML
   namespace</a>.<li><p>Let <var>head</var> be the result of <a href=https://dom.spec.whatwg.org/#concept-create-element id=shared-document-creation-infrastructure:create-an-element-2 data-x-internal=create-an-element>creating an
   element</a> given <var>document</var>, "<code>head</code>", and the <a id=shared-document-creation-infrastructure:html-namespace-2-2 href=https://infra.spec.whatwg.org/#html-namespace data-x-internal=html-namespace-2>HTML
   namespace</a>.<li><p>Let <var>body</var> be the result of <a href=https://dom.spec.whatwg.org/#concept-create-element id=shared-document-creation-infrastructure:create-an-element-3 data-x-internal=create-an-element>creating an
   element</a> given <var>document</var>, "<code>body</code>", and the <a id=shared-document-creation-infrastructure:html-namespace-2-3 href=https://infra.spec.whatwg.org/#html-namespace data-x-internal=html-namespace-2>HTML
   namespace</a>.<li><p><a href=https://dom.spec.whatwg.org/#concept-node-append id=shared-document-creation-infrastructure:concept-node-append data-x-internal=concept-node-append>Append</a> <var>html</var> to
   <var>document</var>.<li><p><a href=https://dom.spec.whatwg.org/#concept-node-append id=shared-document-creation-infrastructure:concept-node-append-2 data-x-internal=concept-node-append>Append</a> <var>head</var> to
   <var>html</var>.<li><p><a href=https://dom.spec.whatwg.org/#concept-node-append id=shared-document-creation-infrastructure:concept-node-append-3 data-x-internal=concept-node-append>Append</a> <var>body</var> to
   <var>html</var>.</ol>


  <h4 id=read-html><span class=secno>7.5.2</span> Loading HTML documents<a href=#read-html class=self-link></a></h4>

  <p>To <dfn id=navigate-html>load an HTML document</dfn>, given <a id=read-html:navigation-params href=browsing-the-web.html#navigation-params>navigation
  params</a> <var>navigationParams</var>:</p>

  <ol><li><p>Let <var>document</var> be the result of <a href=#initialise-the-document-object id=read-html:initialise-the-document-object>creating and initializing a <code>Document</code>
   object</a> given "<code>html</code>", "<code>text/html</code>", and
   <var>navigationParams</var>.<li>
    <p>If <var>document</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-url id="read-html:the-document's-address" data-x-internal="the-document's-address">URL</a> is
    <code id=read-html:about:blank><a href=infrastructure.html#about:blank>about:blank</a></code>, then <a href=#populate-with-html/head/body id=read-html:populate-with-html/head/body>populate with
    <code>html</code>/<code>head</code>/<code>body</code></a> given <var>document</var>.</p>

    <p class=note>This special case, where even non-<a href=dom.html#is-initial-about:blank id=read-html:is-initial-about:blank>initial
    <code>about:blank</code></a> <code>Document</code>s are synchronously given their element
    nodes, is necessary for compatible with deployed content. In other words, it is not compatible
    to instead go down the "otherwise" branch and feed the empty <a id=read-html:byte-sequence href=https://infra.spec.whatwg.org/#byte-sequence data-x-internal=byte-sequence>byte sequence</a> into
    an <span>HTML parser</span> to asynchronously populate <var>document</var>.</p>
   <li>
    <p>Otherwise, create an <span>HTML parser</span> and associate it with the <var>document</var>.
    Each <span>task</span> that the <span>networking task source</span>
    places on the <span>task queue</span> while fetching runs must then fill the parser's
    <span>input byte stream</span> with the fetched bytes and cause the <span>HTML parser</span> to
    perform the appropriate processing of the input stream.</p>

    <p>The first <span>task</span> that the <span>networking task
    source</span> places on the <span>task queue</span> while fetching runs must <span>process link
    headers</span> given <var>document</var>, <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-html:navigation-params-response>response</a>, and "<code>media</code>", after
    the task has been processed by the <span>HTML parser</span>.</p>

    <p>Before any script execution occurs, the user agent must wait for <a id=read-html:scripts-may-run-for-the-newly-created-document href=browsing-the-web.html#scripts-may-run-for-the-newly-created-document>scripts may run for
    the newly-created document</a> to be true for <var>document</var>.</p>

    <p class=note>The <span>input byte stream</span> converts bytes into characters for use in the
    <span>tokenizer</span>. This process relies, in part, on character encoding
    information found in the real <span>Content-Type metadata</span> of the
    resource; the computed type is not used for this purpose.</p>

    <p>When no more bytes are available, the user agent must <span>queue a global task</span> on
    the <span>networking task source</span> given <var>document</var>'s <span>relevant global
    object</span> to have the parser process the implied EOF character, which eventually causes
    a <code id=read-html:event-load><a href=indices.html#event-load>load</a></code> event to be fired.</p>
   <li><p>Return <var>document</var>.</ol>


  <h4 id=read-xml><span class=secno>7.5.3</span> <dfn>Loading XML documents</dfn><a href=#read-xml class=self-link></a></h4>

  <p>When faced with displaying an XML file inline, provided <a id=read-xml:navigation-params href=browsing-the-web.html#navigation-params>navigation params</a>
  <var>navigationParams</var> and a string <var>type</var>, user agents must follow the
  requirements defined in <cite>XML</cite> and <cite>Namespaces in XML</cite>, <cite>XML Media
  Types</cite>, <cite>DOM</cite>, and other relevant specifications to <a href=#initialise-the-document-object id=read-xml:initialise-the-document-object>create and initialize a <code>Document</code> object</a>
  <var>document</var>, given "<code>xml</code>", <var>type</var>, and
  <var>navigationParams</var>, and return that <code>Document</code>. They must also create a
  corresponding <span>XML parser</span>. <a href=references.html#refsXML>[XML]</a> <a href=references.html#refsXMLNS>[XMLNS]</a> <a href=references.html#refsRFC7303>[RFC7303]</a>
  <a href=references.html#refsDOM>[DOM]</a></p>

  <p class=note>At the time of writing, the XML specification community had not actually yet
  specified how XML and the DOM interact.</p> 

  <p>The first <span>task</span> that the <span>networking task source</span>
  places on the <span>task queue</span> while fetching runs must <span>process link headers</span>
  given <var>document</var>, <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-xml:navigation-params-response>response</a>, and "<code>media</code>", after
  the task has been processed by the <span>XML parser</span>.</p>

  <p>The actual HTTP headers and other metadata, not the headers as mutated or implied by the
  algorithms given in this specification, are the ones that must be used when determining the
  character encoding according to the rules given in the above specifications. Once the character
  encoding is established, the <a id="read-xml:document's-character-encoding" href=https://dom.spec.whatwg.org/#concept-document-encoding data-x-internal="document's-character-encoding">document's character encoding</a> must be set to that
  character encoding.</p>

  <p>Before any script execution occurs, the user agent must wait for <a id=read-xml:scripts-may-run-for-the-newly-created-document href=browsing-the-web.html#scripts-may-run-for-the-newly-created-document>scripts may run for the
  newly-created document</a> to be true for the newly-created <code>Document</code>.</p>

  <p>Once parsing is complete, the user agent must set <var>document</var>'s <a href=dom.html#concept-document-navigation-id id=read-xml:concept-document-navigation-id>during-loading navigation ID for WebDriver BiDi</a> to
  null.</p>

  <p class=note>For HTML documents this is reset when parsing is complete, after firing the load
  event.</p>

  <p>Error messages from the parse process (e.g., XML namespace well-formedness errors) may be
  reported inline by mutating the <code>Document</code>.</p>


  <h4 id=read-text><span class=secno>7.5.4</span> Loading text documents<a href=#read-text class=self-link></a></h4>

  <p>To <dfn id=navigate-text>load a text document</dfn>, given a <a id=read-text:navigation-params href=browsing-the-web.html#navigation-params>navigation
  params</a> <var>navigationParams</var> and a string <var>type</var>:</p>

  <ol><li><p>Let <var>document</var> be the result of <a href=#initialise-the-document-object id=read-text:initialise-the-document-object>creating and initializing a <code>Document</code>
   object</a> given "<code>html</code>", <var>type</var>, and
   <var>navigationParams</var>.<li><p>Set <var>document</var>'s <span>parser cannot change the mode flag</span> to
   true.<li><p>Set <var>document</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-mode id=read-text:concept-document-mode data-x-internal=concept-document-mode>mode</a>
   to "<code>no-quirks</code>".<li>
    <p>Create an <span>HTML parser</span> and associate it with the <var>document</var>. Act as
    if the tokenizer had emitted a start tag token with the tag name "pre" followed by a single
    U+000A LINE FEED (LF) character, and switch the <span>HTML parser</span>'s tokenizer to the
    <span>PLAINTEXT state</span>. Each <span>task</span> that the
    <span>networking task source</span> places on the <span>task queue</span> while fetching runs
    must then fill the parser's <span>input byte stream</span> with the fetched bytes and cause the
    <span>HTML parser</span> to perform the appropriate processing of the input stream.</p>

    <p><var>document</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-encoding id="read-text:document's-character-encoding" data-x-internal="document's-character-encoding">encoding</a> must be
    set to the character encoding used to decode the document during parsing.</p>

    <p>The first <span>task</span> that the <span>networking task
    source</span> places on the <span>task queue</span> while fetching runs must <span>process link
    headers</span> given <var>document</var>, <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-text:navigation-params-response>response</a>, and "<code>media</code>", after
    the task has been processed by the <span>HTML parser</span>.</p>

    <p>Before any script execution occurs, the user agent must wait for <a id=read-text:scripts-may-run-for-the-newly-created-document href=browsing-the-web.html#scripts-may-run-for-the-newly-created-document>scripts may run for
    the newly-created document</a> to be true for <var>document</var>.</p>

    <p>When no more bytes are available, the user agent must <span>queue a global task</span> on
    the <span>networking task source</span> given <var>document</var>'s <span>relevant global
    object</span> to have the parser process the implied EOF character, which eventually causes
    a <code id=read-text:event-load><a href=indices.html#event-load>load</a></code> event to be fired.</p>
   <li>
    <p>User agents may add content to the <code id=read-text:the-head-element><a href=semantics.html#the-head-element>head</a></code> element of <var>document</var>, e.g.,
    linking to a style sheet, providing script, or giving the document a <code id=read-text:the-title-element><a href=semantics.html#the-title-element>title</a></code>.</p>

    <p class=note>In particular, if the user agent supports the <code>Format=Flowed</code> feature of RFC 3676 then the user agent would need to apply
    extra styling to cause the text to wrap correctly and to handle the quoting feature. This could
    be performed using, e.g., a CSS extension.</p>
   <li><p>Return <var>document</var>.</ol>

  <p>The rules for how to convert the bytes of the plain text document into actual characters, and
  the rules for actually rendering the text to the user, are defined by the specifications for the
  <span>computed MIME type</span> of the resource (i.e.,
  <var>type</var>).</p>

  <h4 id=read-multipart-x-mixed-replace><span class=secno>7.5.5</span> Loading <code>multipart/x-mixed-replace</code>
  documents<a href=#read-multipart-x-mixed-replace class=self-link></a></h4>

  <p>To <dfn id=navigate-multipart-x-mixed-replace>load a
  <code>multipart/x-mixed-replace</code> document</dfn>, given <a id=read-multipart-x-mixed-replace:navigation-params href=browsing-the-web.html#navigation-params>navigation params</a>
  <var>navigationParams</var>, <a id=read-multipart-x-mixed-replace:source-snapshot-params href=browsing-the-web.html#source-snapshot-params>source snapshot params</a> <var>sourceSnapshotParams</var>,
  and <a id=read-multipart-x-mixed-replace:concept-origin href=browsers.html#concept-origin>origin</a> <var>initiatorOrigin</var>:</p>

  <ol><li><p>Parse <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-multipart-x-mixed-replace:navigation-params-response>response</a>'s <a href=https://fetch.spec.whatwg.org/#concept-response-body id=read-multipart-x-mixed-replace:concept-response-body data-x-internal=concept-response-body>body</a> using the rules for multipart types.
   <a href=references.html#refsRFC2046>[RFC2046]</a><li><p>Let <var>firstPartNavigationParams</var> be a copy of
   <var>navigationParams</var>.<li><p>Set <var>firstPartNavigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-multipart-x-mixed-replace:navigation-params-response-2>response</a> to a new <a href=https://fetch.spec.whatwg.org/#concept-response id=read-multipart-x-mixed-replace:concept-response data-x-internal=concept-response>response</a> representing the first part of
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-multipart-x-mixed-replace:navigation-params-response-3>response</a>'s <a href=https://fetch.spec.whatwg.org/#concept-response-body id=read-multipart-x-mixed-replace:concept-response-body-2 data-x-internal=concept-response-body>body</a>'s multipart stream.<li>
    <p>Let <var>document</var> be the result of <a id=read-multipart-x-mixed-replace:loading-a-document href=browsing-the-web.html#loading-a-document>loading a document</a> given
    <var>firstPartNavigationParams</var>, <var>sourceSnapshotParams</var>, and
    <var>initiatorOrigin</var>.</p>

    <p>For each additional body part obtained from <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-multipart-x-mixed-replace:navigation-params-response-4>response</a>, the user agent must <a id=read-multipart-x-mixed-replace:navigate href=browsing-the-web.html#navigate>navigate</a>
    <var>document</var>'s <a id=read-multipart-x-mixed-replace:node-navigable href=document-sequences.html#node-navigable>node navigable</a> to <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-request id=read-multipart-x-mixed-replace:navigation-params-request>request</a>'s <a href=https://fetch.spec.whatwg.org/#concept-request-url id=read-multipart-x-mixed-replace:concept-request-url data-x-internal=concept-request-url>URL</a>, using <var>document</var>, with <i id=read-multipart-x-mixed-replace:navigation-response><a href=browsing-the-web.html#navigation-response>response</a></i> set to <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-multipart-x-mixed-replace:navigation-params-response-5>response</a> and <i id=read-multipart-x-mixed-replace:navigation-hh><a href=browsing-the-web.html#navigation-hh>historyHandling</a></i> set to "<code id=read-multipart-x-mixed-replace:navigationhistorybehavior-replace-2><a href=browsing-the-web.html#navigationhistorybehavior-replace-2>replace</a></code>".</p>
   <li><p>Return <var>document</var>.</ol>

  <p>For the purposes of algorithms processing these body parts as if they were complete
  stand-alone resources, the user agent must act as if there were no more bytes for those resources
  whenever the boundary following the body part is reached.</p>

  <p class=note>Thus, <code id=read-multipart-x-mixed-replace:event-load><a href=indices.html#event-load>load</a></code> events (and for that matter <code id=read-multipart-x-mixed-replace:event-unload><a href=indices.html#event-unload>unload</a></code> events) do fire for each body part loaded.</p>


  <h4 id=read-media><span class=secno>7.5.6</span> Loading media documents<a href=#read-media class=self-link></a></h4>

  <p>To <dfn id=navigate-media>load a media document</dfn>, given <var>navigationParams</var>
  and a string <var>type</var>:</p>

  <ol><li><p>Let <var>document</var> be the result of <a href=#initialise-the-document-object id=read-media:initialise-the-document-object>creating and initializing a <code>Document</code>
   object</a> given "<code>html</code>", <var>type</var>, and
   <var>navigationParams</var>.<li><p>Set <var>document</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-mode id=read-media:concept-document-mode data-x-internal=concept-document-mode>mode</a>
   to "<code>no-quirks</code>".<li><p><a href=#populate-with-html/head/body id=read-media:populate-with-html/head/body>Populate with <code>html</code>/<code>head</code>/<code>body</code></a> given
   <var>document</var>.<li><p>Append an element <var>host element</var> for the media, as described below, to the
   <code id=read-media:the-body-element><a href=sections.html#the-body-element>body</a></code> element.<li><p>Set the appropriate attribute of the element <var>host element</var>, as described below,
   to the address of the image, video, or audio resource.<li><p>User agents may add content to the <code id=read-media:the-head-element><a href=semantics.html#the-head-element>head</a></code> element of <var>document</var>, or
   attributes to <var>host element</var>, e.g., to link to a style sheet, to provide a script, to
   give the document a <code id=read-media:the-title-element><a href=semantics.html#the-title-element>title</a></code>, or to make the media <a href=media.html#attr-media-autoplay id=read-media:attr-media-autoplay>autoplay</a>.<li><p><span>Process link headers</span> given <var>document</var>,
   <var>navigationParams</var>'s <a href=browsing-the-web.html#navigation-params-response id=read-media:navigation-params-response>response</a>, and
   "<code>media</code>".<li><p>Act as if the user agent had <a href=dom.html#stop-parsing id=read-media:stop-parsing>stopped parsing</a>
   <var>document</var>.<li><p>Return <var>document</var>.</ol>

  <p>The element <var>host element</var> to create for the media is the element given in
  the table below in the second cell of the row whose first cell describes the media. The
  appropriate attribute to set is the one given by the third cell in that same row.</p>

  <table><thead><tr><th> Type of media
         <th> Element for the media
         <th> Appropriate attribute
   <tbody><tr><td> Image
         <td> <code id=read-media:the-img-element><a href=embedded-content.html#the-img-element>img</a></code>
         <td> <code id=read-media:attr-img-src><a href=embedded-content.html#attr-img-src>src</a></code>
    <tr><td> Video
         <td> <code id=read-media:the-video-element><a href=media.html#the-video-element>video</a></code>
         <td> <code id=read-media:attr-media-src><a href=media.html#attr-media-src>src</a></code>
    <tr><td> Audio
         <td> <code id=read-media:the-audio-element><a href=media.html#the-audio-element>audio</a></code>
         <td> <code id=read-media:attr-media-src-2><a href=media.html#attr-media-src>src</a></code>
  </table>

  <p>Before any script execution occurs, the user agent must wait for <a id=read-media:scripts-may-run-for-the-newly-created-document href=browsing-the-web.html#scripts-may-run-for-the-newly-created-document>scripts may run for the
  newly-created document</a> to be true for the <code>Document</code>.</p>


  <h4 id=read-ua-inline><span class=secno>7.5.7</span> <span id=read-ua-plugin></span><span id=navigate-plugin></span><dfn>Loading a document for inline content that doesn't have a
  DOM</dfn><a href=#read-ua-inline class=self-link></a></h4>

  <p>When the user agent is to create a document to display a user agent page or PDF viewer inline,
  provided a <a id=read-ua-inline:navigable href=document-sequences.html#navigable>navigable</a> <var>navigable</var>, a <a id=read-ua-inline:navigation-id href=browsing-the-web.html#navigation-id>navigation ID</a>
  <var>navigationId</var>, a <code id=read-ua-inline:navigationtimingtype><a data-x-internal=navigationtimingtype href=https://w3c.github.io/navigation-timing/#dom-navigationtimingtype>NavigationTimingType</a></code> <var>navTimingType</var>, and a
  <a id=read-ua-inline:user-navigation-involvement href=browsing-the-web.html#user-navigation-involvement>user navigation involvement</a> <var>userInvolvement</var>, the user agent should:</p>

  <ol><li><p>Let <var>origin</var> be a new <a href=browsers.html#concept-origin-opaque id=read-ua-inline:concept-origin-opaque>opaque
   origin</a>.<li><p>Let <var>coop</var> be a new <span>opener policy</span>.<li>
    <p>Let <var>coopEnforcementResult</var> be a new <span>opener
    policy enforcement result</span> with</p>

    <dl class=props><dt><span>url</span><dd><var>response</var>'s <a href=https://fetch.spec.whatwg.org/#concept-response-url id=read-ua-inline:concept-response-url data-x-internal=concept-response-url>URL</a><dt><span>origin</span><dd><var>origin</var><dt><span>opener policy</span><dd><var>coop</var></dl>
   <li>
    <p>Let <var>navigationParams</var> be a new <a id=read-ua-inline:navigation-params href=browsing-the-web.html#navigation-params>navigation params</a> with</p>

    <dl class=props><dt><a href=browsing-the-web.html#navigation-params-id id=read-ua-inline:navigation-params-id>id</a><dd><var>navigationId</var><dt><a href=browsing-the-web.html#navigation-params-navigable id=read-ua-inline:navigation-params-navigable>navigable</a><dd><var>navigable</var><dt><a href=browsing-the-web.html#navigation-params-request id=read-ua-inline:navigation-params-request>request</a><dd>null<dt><a href=browsing-the-web.html#navigation-params-response id=read-ua-inline:navigation-params-response>response</a><dd>a new <a href=https://fetch.spec.whatwg.org/#concept-response id=read-ua-inline:concept-response data-x-internal=concept-response>response</a><dt><a href=browsing-the-web.html#navigation-params-origin id=read-ua-inline:navigation-params-origin>origin</a><dd><var>origin</var><dt><a href=browsing-the-web.html#navigation-params-fetch-controller id=read-ua-inline:navigation-params-fetch-controller>fetch controller</a><dd>null<dt><a href=browsing-the-web.html#navigation-params-commit-early-hints id=read-ua-inline:navigation-params-commit-early-hints>commit early hints</a><dd>null<dt><a href=browsing-the-web.html#navigation-params-coop-enforcement-result id=read-ua-inline:navigation-params-coop-enforcement-result>COOP enforcement result</a><dd><var>coopEnforcementResult</var><dt><a href=browsing-the-web.html#navigation-params-reserved-environment id=read-ua-inline:navigation-params-reserved-environment>reserved environment</a><dd>null<dt><a href=browsing-the-web.html#navigation-params-policy-container id=read-ua-inline:navigation-params-policy-container>policy container</a><dd>a new <span>policy container</span><dt><a href=browsing-the-web.html#navigation-params-sandboxing id=read-ua-inline:navigation-params-sandboxing>final sandboxing flag set</a><dd>an empty set<dt><a href=browsing-the-web.html#navigation-params-coop id=read-ua-inline:navigation-params-coop>opener policy</a><dd><var>coop</var><dt><a href=browsing-the-web.html#navigation-params-nav-timing-type id=read-ua-inline:navigation-params-nav-timing-type>navigation timing type</a><dd><var>navTimingType</var><dt><a href=browsing-the-web.html#navigation-params-about-base-url id=read-ua-inline:navigation-params-about-base-url>about base URL</a><dd>null<dt><a href=browsing-the-web.html#navigation-params-user-involvement id=read-ua-inline:navigation-params-user-involvement>user involvement</a><dd><var>userInvolvement</var></dl>
   <li><p>Let <var>document</var> be the result of <a href=#initialise-the-document-object id=read-ua-inline:initialise-the-document-object>creating and initializing a <code>Document</code>
   object</a> given "<code>html</code>", "<code>text/html</code>", and
   <var>navigationParams</var>.<li><p>Either associate <var>document</var> with a custom rendering that is not rendered using
   the normal <code>Document</code> rendering rules, or mutate <var>document</var> until it
   represents the content the user agent wants to render.<li><p>Return <var>document</var>.</ol>

  <p class=note>Because we ensure the resulting <code>Document</code>'s <a href=https://dom.spec.whatwg.org/#concept-document-origin id=read-ua-inline:concept-document-origin data-x-internal=concept-document-origin>origin</a> is <a href=browsers.html#concept-origin-opaque id=read-ua-inline:concept-origin-opaque-2>opaque</a>, and the resulting <code>Document</code> cannot run
  script with access to the DOM, the existence and properties of this <code>Document</code> are not
  observable to web developer code. This means that most of the above values, e.g., the
  <code>text/html</code> <a href=https://dom.spec.whatwg.org/#concept-document-type id=read-ua-inline:concept-document-type data-x-internal=concept-document-type>type</a>, do not matter.
  Similarly, most of the items in <var>navigationParams</var> don't have any observable effect,
  besides preventing the <a href=#initialise-the-document-object id=read-ua-inline:initialise-the-document-object-2><code>Document</code>-creation
  algorithm</a> from getting confused, and so are set to default values.</p>

  <p>Once the page has been set up, the user agent must act as if it had <a href=dom.html#stop-parsing id=read-ua-inline:stop-parsing>stopped parsing</a>.</p>


  <h4 id=loading-documents><span class=secno>7.5.8</span> Finishing the loading process<a href=#loading-documents class=self-link></a></h4>

  <p>A <code>Document</code> has a <dfn id=completely-loaded-time>completely loaded time</dfn> (a time or null), which is
  initially null.</p>

  <p>A <code>Document</code> is considered <dfn id=completely-loaded>completely loaded</dfn> if its <a href=#completely-loaded-time id=loading-documents:completely-loaded-time>completely
  loaded time</a> is non-null.

  <p>To <dfn id=completely-finish-loading>completely finish loading</dfn> a <code>Document</code> <var>document</var>:</p>

  <ol><li><p><a id=loading-documents:assert href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: <var>document</var>'s <a href=document-sequences.html#concept-document-bc id=loading-documents:concept-document-bc>browsing
   context</a> is non-null.<li><p>Set <var>document</var>'s <a href=#completely-loaded-time id=loading-documents:completely-loaded-time-2>completely loaded time</a> to the current
   time.<li>
    <p>Let <var>container</var> be <var>document</var>'s <a id=loading-documents:node-navigable href=document-sequences.html#node-navigable>node navigable</a>'s <a href=document-sequences.html#nav-container id=loading-documents:nav-container>container</a>.</p>

    <div class=note>
     <p>This will be null in the case where <var>document</var> is the <a href=dom.html#is-initial-about:blank id=loading-documents:is-initial-about:blank>initial <code>about:blank</code></a> <code>Document</code> in a
     <code>frame</code> or <code id=loading-documents:the-iframe-element><a href=iframe-embed-object.html#the-iframe-element>iframe</a></code>, since at the point of <a href=document-sequences.html#creating-a-new-browsing-context id=loading-documents:creating-a-new-browsing-context>browsing context creation</a> which calls this algorithm,
     the container relationship has not yet been established. (That happens in a subsequent step of
     <a id=loading-documents:create-a-new-child-navigable href=document-sequences.html#create-a-new-child-navigable>create a new child navigable</a>.)</p>

     <p>The consequence of this is that the following steps do nothing, i.e., we do not fire an
     asynchronous <code id=loading-documents:event-load><a href=indices.html#event-load>load</a></code> event on the container element for such
     cases. Instead, a synchronous <code id=loading-documents:event-load-2><a href=indices.html#event-load>load</a></code> event is fired in a special
     initial-insertion case when <span>processing the
     <code id=loading-documents:the-iframe-element-2><a href=iframe-embed-object.html#the-iframe-element>iframe</a></code> attributes</span>.</p>
    </div>
   <li><p>If <var>container</var> is an <code id=loading-documents:the-iframe-element-3><a href=iframe-embed-object.html#the-iframe-element>iframe</a></code> element, then <span>queue an element
   task</span> on the <span>DOM manipulation task source</span> given <var>container</var> to run
   the <span>iframe load event steps</span> given <var>container</var>.<li><p>Otherwise, if <var>container</var> is non-null, then <span>queue an element task</span> on
   the <span>DOM manipulation task source</span> given <var>container</var> to <a href=https://dom.spec.whatwg.org/#concept-event-fire id=loading-documents:concept-event-fire data-x-internal=concept-event-fire>fire an event</a> named <code id=loading-documents:event-load-3><a href=indices.html#event-load>load</a></code> at
   <var>container</var>.</p>
  </ol>

  <h4 id=unloading-documents><span class=secno>7.5.9</span> Unloading documents<a href=#unloading-documents class=self-link></a></h4>

  <p>A <code>Document</code> has a <dfn id=concept-document-salvageable><i>salvageable</i></dfn> state, which must initially be
  true, and a <dfn id=page-showing>page showing</dfn> boolean, which is initially false. The <a href=#page-showing id=unloading-documents:page-showing>page
  showing</a> boolean is used to ensure that scripts receive <code id=unloading-documents:event-pageshow><a href=indices.html#event-pageshow>pageshow</a></code> and <code id=unloading-documents:event-pagehide><a href=indices.html#event-pagehide>pagehide</a></code> events
  in a consistent manner (e.g., that they never receive two <code id=unloading-documents:event-pagehide-2><a href=indices.html#event-pagehide>pagehide</a></code> events in a row without an intervening <code id=unloading-documents:event-pageshow-2><a href=indices.html#event-pageshow>pageshow</a></code>, or vice versa).</p>

  <p>A <code>Document</code> has a <code id=unloading-documents:domhighrestimestamp><a data-x-internal=domhighrestimestamp href=https://w3c.github.io/hr-time/#dom-domhighrestimestamp>DOMHighResTimeStamp</a></code> <dfn id=suspension-time>suspension time</dfn>,
  initially 0.</p>

  <p>A <code>Document</code> has a <a id=unloading-documents:list href=https://infra.spec.whatwg.org/#list data-x-internal=list>list</a> of <dfn id=suspended-timer-handles>suspended timer handles</dfn>,
  initially empty.</p>

  <p><a href=webappapis.html#event-loop id=unloading-documents:event-loop>Event loops</a> have a <dfn id=termination-nesting-level>termination nesting level</dfn>
  counter, which must initially be 0.</p>

  <p id=ignore-opens-during-unload-counter><code>Document</code> objects have an <dfn id=unload-counter>unload
  counter</dfn>, which is used to ignore certain operations while the below algorithms run.
  Initially, the counter must be set to zero.</p>

  <p>To <dfn id=unload-a-document>unload</dfn> a <code>Document</code>
  <var>oldDocument</var>, given an optional <code>Document</code> <var>newDocument</var>:</p>

  <ol><li><p><a id=unloading-documents:assert href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: this is running as part of a <span>task</span>
   queued on <var>oldDocument</var>'s <span>relevant agent</span>'s <a href=webappapis.html#concept-agent-event-loop id=unloading-documents:concept-agent-event-loop>event loop</a>.<li><p>Let <var>unloadTimingInfo</var> be a new <span>document unload timing
   info</span>.<li>
    <p>If <var>newDocument</var> is not given, then set <var>unloadTimingInfo</var> to null.</p>

    <p class=note>In this case there is no new document that needs to know about how long it took
    <var>oldDocument</var> to unload.</p>
   <li>
    <p>Otherwise, if <var>newDocument</var>'s <a id=unloading-documents:event-loop-2 href=webappapis.html#event-loop>event loop</a> is not
    <var>oldDocument</var>'s <a id=unloading-documents:event-loop-3 href=webappapis.html#event-loop>event loop</a>, then the user agent may be <a href=#unload-a-document id=unloading-documents:unload-a-document>unloading</a> <var>oldDocument</var> <span>in parallel</span>. In
    that case, the user agent should set <var>unloadTimingInfo</var> to null.</p>

    <p class=note>In this case <var>newDocument</var>'s loading is not impacted by how long it
    takes to unload <var>oldDocument</var>, so it would be meaningless to communicate that timing
    info.</p>
   <li>
    <p>Let <var>intendToKeepInBfcache</var> be true if the user agent intends to keep
    <var>oldDocument</var> alive in a <a id=unloading-documents:session-history-entry href=browsing-the-web.html#session-history-entry>session history entry</a>, such that it can later be
    <a href=browsing-the-web.html#note-bfcache>used for history traversal</a>.</p>

    <p>This must be false if <var>oldDocument</var> is not <i id=unloading-documents:concept-document-salvageable><a href=#concept-document-salvageable>salvageable</a></i>, or if there are any descendants of
    <var>oldDocument</var> which the user agent does not intend to keep alive in the same way
    (including due to their lack of <i id=unloading-documents:concept-document-salvageable-2><a href=#concept-document-salvageable>salvageability</a></i>).</p>
   <li><p>Let <var>eventLoop</var> be <var>oldDocument</var>'s <span>relevant agent</span>'s <a href=webappapis.html#concept-agent-event-loop id=unloading-documents:concept-agent-event-loop-2>event loop</a>.<li><p>Increase <var>eventLoop</var>'s <a href=#termination-nesting-level id=unloading-documents:termination-nesting-level>termination nesting level</a> by 1.<li><p>Increase <var>oldDocument</var>'s <a href=#unload-counter id=unloading-documents:unload-counter>unload counter</a> by 1.<li><p>If <var>intendToKeepInBfcache</var> is false, then set <var>oldDocument</var>'s <i id=unloading-documents:concept-document-salvageable-3><a href=#concept-document-salvageable>salvageable</a></i> state to false.<li>
    <p>If <var>oldDocument</var>'s <a href=#page-showing id=unloading-documents:page-showing-2>page showing</a> is true:</p>

    <ol><li><p>Set <var>oldDocument</var>'s <a href=#page-showing id=unloading-documents:page-showing-3>page showing</a> to false.<li><p><span>Fire a page transition event</span> named <code id=unloading-documents:event-pagehide-3><a href=indices.html#event-pagehide>pagehide</a></code> at <var>oldDocument</var>'s <span>relevant global
     object</span> with <var>oldDocument</var>'s <i id=unloading-documents:concept-document-salvageable-4><a href=#concept-document-salvageable>salvageable</a></i> state.<li><p><a id=unloading-documents:update-the-visibility-state href=interaction.html#update-the-visibility-state>Update the visibility state</a> of <var>oldDocument</var> to
     "<code>hidden</code>".</ol>
   <li><p>If <var>unloadTimingInfo</var> is not null, then set <var>unloadTimingInfo</var>'s
   <span>unload event start time</span> to the <a id=unloading-documents:current-high-resolution-time href=https://w3c.github.io/hr-time/#dfn-current-high-resolution-time data-x-internal=current-high-resolution-time>current high resolution time</a> given
   <var>newDocument</var>'s <span>relevant global object</span>, <a href=https://w3c.github.io/hr-time/#dfn-coarsen-time id=unloading-documents:coarsen-time data-x-internal=coarsen-time>coarsened</a> given <var>oldDocument</var>'s <span>relevant settings object</span>'s
   <span>cross-origin isolated
   capability</span>.<li><p>If <var>oldDocument</var>'s <i id=unloading-documents:concept-document-salvageable-5><a href=#concept-document-salvageable>salvageable</a></i>
   state is false, then <a href=https://dom.spec.whatwg.org/#concept-event-fire id=unloading-documents:concept-event-fire data-x-internal=concept-event-fire>fire an event</a> named <code id=unloading-documents:event-unload><a href=indices.html#event-unload>unload</a></code> at <var>oldDocument</var>'s <span>relevant global
   object</span>, with <i>legacy target override flag</i> set.<li><p>If <var>unloadTimingInfo</var> is not null, then set <var>unloadTimingInfo</var>'s
   <span>unload event end time</span> to the <a id=unloading-documents:current-high-resolution-time-2 href=https://w3c.github.io/hr-time/#dfn-current-high-resolution-time data-x-internal=current-high-resolution-time>current high resolution time</a> given
   <var>newDocument</var>'s <span>relevant global object</span>, <a href=https://w3c.github.io/hr-time/#dfn-coarsen-time id=unloading-documents:coarsen-time-2 data-x-internal=coarsen-time>coarsened</a> given <var>oldDocument</var>'s <span>relevant settings object</span>'s
   <span>cross-origin isolated
   capability</span>.<li><p>Decrease <var>eventLoop</var>'s <a href=#termination-nesting-level id=unloading-documents:termination-nesting-level-2>termination nesting level</a> by 1.<li><p>Set <var>oldDocument</var>'s <a href=#suspension-time id=unloading-documents:suspension-time>suspension time</a> to the <a id=unloading-documents:current-high-resolution-time-3 href=https://w3c.github.io/hr-time/#dfn-current-high-resolution-time data-x-internal=current-high-resolution-time>current high
   resolution time</a> given <var>document</var>'s <span>relevant global object</span>.<li><p>Set <var>oldDocument</var>'s <a href=#suspended-timer-handles id=unloading-documents:suspended-timer-handles>suspended timer handles</a> to the result of
   <a href=https://infra.spec.whatwg.org/#map-getting-the-keys id=unloading-documents:map-get-the-keys data-x-internal=map-get-the-keys>getting the keys</a> for the
   <span>map of active timers</span>.<li><p>Set <var>oldDocument</var>'s <a id=unloading-documents:has-been-scrolled-by-the-user href=browsing-the-web.html#has-been-scrolled-by-the-user>has been scrolled by the user</a> to false.<li><p>Run any <a href=#unloading-document-cleanup-steps id=unloading-documents:unloading-document-cleanup-steps>unloading document cleanup steps</a> for <var>oldDocument</var> that are
   defined by this specification and <a id=unloading-documents:other-applicable-specifications href=infrastructure.html#other-applicable-specifications>other applicable specifications</a>.<li><p>If <var>oldDocument</var>'s <a id=unloading-documents:node-navigable href=document-sequences.html#node-navigable>node navigable</a> is a <a id=unloading-documents:top-level-traversable href=document-sequences.html#top-level-traversable>top-level
   traversable</a>, <a id=unloading-documents:build-not-restored-reasons-for-a-top-level-traversable-and-its-descendants href=browsing-the-web.html#build-not-restored-reasons-for-a-top-level-traversable-and-its-descendants>build not restored reasons for a top-level traversable and its
   descendants</a> given <var>oldDocument</var>'s <a id=unloading-documents:node-navigable-2 href=document-sequences.html#node-navigable>node navigable</a>.<li><p>If <var>oldDocument</var>'s <i id=unloading-documents:concept-document-salvageable-6><a href=#concept-document-salvageable>salvageable</a></i> state
   is false, then <a href=#destroy-a-document id=unloading-documents:destroy-a-document>destroy</a> <var>oldDocument</var>.<li><p>Decrease <var>oldDocument</var>'s <a href=#unload-counter id=unloading-documents:unload-counter-2>unload counter</a> by 1.<li><p>If <var>newDocument</var> is given, <var>newDocument</var>'s <span>was created via
   cross-origin redirects</span> is false, and <var>newDocument</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-origin id=unloading-documents:concept-document-origin data-x-internal=concept-document-origin>origin</a> is the <a href=browsers.html#same-origin id=unloading-documents:same-origin>same</a> as
   <var>oldDocument</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-origin id=unloading-documents:concept-document-origin-2 data-x-internal=concept-document-origin>origin</a>, then set
   <var>newDocument</var>'s <span>previous document unload timing</span> to
   <var>unloadTimingInfo</var>.</ol>

  <p>To <dfn id=unload-a-document-and-its-descendants>unload a document and its descendants</dfn>, given a <code>Document</code>
  <var>document</var>, an optional <code>Document</code>-or-null <var>newDocument</var> (default
  null), an optional set of steps <var>afterAllUnloads</var>, and an optional set of steps
  <var>firePageSwapSteps</var>:</p>

  <ol><li><p><a id=unloading-documents:assert-2 href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: this is running within <var>document</var>'s <a id=unloading-documents:node-navigable-3 href=document-sequences.html#node-navigable>node
   navigable</a>'s <a href=document-sequences.html#nav-traversable id=unloading-documents:nav-traversable>traversable navigable</a>'s <a href=document-sequences.html#tn-session-history-traversal-queue id=unloading-documents:tn-session-history-traversal-queue>session history traversal queue</a>.<li><p>Let <var>childNavigables</var> be <var>document</var>'s <a href=document-sequences.html#child-navigable id=unloading-documents:child-navigable>child navigables</a>.<li><p>Let <var>numberUnloaded</var> be 0.<li>
    <p><a href=https://infra.spec.whatwg.org/#list-iterate id=unloading-documents:list-iterate data-x-internal=list-iterate>For each</a> <var>childNavigable</var> of
    <var>childNavigables</var> <span class=XXX>in what order?</span>, <span>queue a
    global task</span> on the <span>navigation and traversal task source</span> given
    <var>childNavigable</var>'s <a href=document-sequences.html#nav-window id=unloading-documents:nav-window>active window</a> to perform the
    following steps:</p>

    <ol><li><p>Let <var>incrementUnloaded</var> be an algorithm step which increments
     <var>numberUnloaded</var>.<li><p><a href=#unload-a-document-and-its-descendants id=unloading-documents:unload-a-document-and-its-descendants>Unload a document and its descendants</a> given <var>childNavigable</var>'s
     <a href=document-sequences.html#nav-document id=unloading-documents:nav-document>active document</a>, null, and
     <var>incrementUnloaded</var>.</ol>
   <li><p>Wait until <var>numberUnloaded</var> equals <var>childNavigable</var>'s <a href=https://infra.spec.whatwg.org/#list-size id=unloading-documents:list-size data-x-internal=list-size>size</a>.<li>
    <p><span>Queue a global task</span> on the <span>navigation and traversal task source</span>
    given <var>document</var>'s <span>relevant global object</span> to perform the following
    steps:</p>

    <ol><li><p>If <var>firePageSwapSteps</var> is given, then run <var>firePageSwapSteps</var>.<li><p><a href=#unload-a-document id=unloading-documents:unload-a-document-2>Unload</a> <var>document</var>, passing along
     <var>newDocument</var> if it is not null.<li><p>If <var>afterAllUnloads</var> was given, then run it.</ol>
   </ol>

  <p>This specification defines the following <dfn id=unloading-document-cleanup-steps data-export="">unloading document cleanup steps</dfn>.
  Other specifications can define more. Given a <code>Document</code> <var>document</var>:</p>

  <ol><li><p>Let <var>window</var> be <var>document</var>'s <span>relevant global
   object</span>.<li>
    <p>For each <code id=unloading-documents:websocket-2><a data-x-internal=websocket-2 href=https://websockets.spec.whatwg.org/#websocket>WebSocket</a></code> object <var>webSocket</var> whose <span>relevant global
    object</span> is <var>window</var>, <a id=unloading-documents:make-disappear href=https://websockets.spec.whatwg.org/#make-disappear data-x-internal=make-disappear>make disappear</a> <var>webSocket</var>.</p>

    <p>If this affected any <code id=unloading-documents:websocket-2-2><a data-x-internal=websocket-2 href=https://websockets.spec.whatwg.org/#websocket>WebSocket</a></code> objects, then <a id=unloading-documents:make-document-unsalvageable href=browsing-the-web.html#make-document-unsalvageable>make document
    unsalvageable</a> given <var>document</var> and "<code id=unloading-documents:blocking-websocket><a href=nav-history-apis.html#blocking-websocket>websocket</a></code>".</p>
   <li><p>For each <code id=unloading-documents:webtransport><a data-x-internal=webtransport href=https://w3c.github.io/webtransport/#webtransport>WebTransport</a></code> object <var>transport</var> whose <span>relevant global
   object</span> is <var>window</var>, run the <a id=unloading-documents:context-cleanup-steps href=https://w3c.github.io/webtransport/#context-cleanup-steps data-x-internal=context-cleanup-steps>context cleanup steps</a> given
   <var>transport</var>.<li>
    <p>If <var>document</var>'s <i id=unloading-documents:concept-document-salvageable-7><a href=#concept-document-salvageable>salvageable</a></i>
    state is false, then:</p>

    <ol><li><p>For each <code>EventSource</code> object <var>eventSource</var> whose <span>relevant
     global object</span> is equal to <var>window</var>, <span>forcibly close</span>
     <var>eventSource</var>.<li><p><a href=https://infra.spec.whatwg.org/#map-clear id=unloading-documents:map-clear data-x-internal=map-clear>Clear</a> <var>window</var>'s
     <span>map of active timers</span>.</ol>
   </ol>

  <div class=XXX>
   <p>It would be better if specification authors sent a pull request to add calls from here into
   their specifications directly, instead of using the <a href=#unloading-document-cleanup-steps id=unloading-documents:unloading-document-cleanup-steps-2>unloading document cleanup
   steps</a> hook, to ensure well-defined cross-specification call order. As of the time of this
   writing the following specifications are known to have <a href=#unloading-document-cleanup-steps id=unloading-documents:unloading-document-cleanup-steps-3>unloading document cleanup
   steps</a>, which will be run in an unspecified order: <cite>Fullscreen API</cite>, <cite>Web
   NFC</cite>, <cite>WebDriver BiDi</cite>, <cite>Compute Pressure</cite>, <cite>File API</cite>,
   <cite>Media Capture and Streams</cite>, <cite>Picture-in-Picture</cite>, <cite>Screen
   Orientation</cite>, <cite>Service Workers</cite>, <cite>WebLocks API</cite>, <cite>WebAudio
   API</cite>, <cite>WebRTC</cite>. <a href=references.html#refsFULLSCREEN>[FULLSCREEN]</a> <a href=references.html#refsWEBNFC>[WEBNFC]</a> <a href=references.html#refsWEBDRIVERBIDI>[WEBDRIVERBIDI]</a>
   <a href=references.html#refsCOMPUTEPRESSURE>[COMPUTEPRESSURE]</a> <a href=references.html#refsFILEAPI>[FILEAPI]</a> <a href=references.html#refsMEDIASTREAM>[MEDIASTREAM]</a> <a href=references.html#refsPICTUREINPICTURE>[PICTUREINPICTURE]</a>
   <a href=references.html#refsSCREENORIENTATION>[SCREENORIENTATION]</a> <a href=references.html#refsSW>[SW]</a> <a href=references.html#refsWEBLOCKS>[WEBLOCKS]</a> <a href=references.html#refsWEBAUDIO>[WEBAUDIO]</a>
   <a href=references.html#refsWEBRTC>[WEBRTC]</a>

   <p><a href=https://github.com/whatwg/html/issues/8906>Issue #8906</a> tracks the work to make
   the order of these steps clear.</p>
  </div>

  <h4 id=destroying-documents><span class=secno>7.5.10</span> Destroying documents<a href=#destroying-documents class=self-link></a></h4>

  <p id=discard-a-document>To <dfn id=destroy-a-document>destroy</dfn> a
  <code>Document</code> <var>document</var>:</p>

  <ol><li><p><a id=destroying-documents:assert href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: this is running as part of a <span>task</span>
   queued on <var>document</var>'s <span>relevant agent</span>'s <a href=webappapis.html#concept-agent-event-loop id=destroying-documents:concept-agent-event-loop>event loop</a>.<li><p><a href=#abort-a-document id=destroying-documents:abort-a-document>Abort</a> <var>document</var>.<li><p>Set <var>document</var>'s <i id=destroying-documents:concept-document-salvageable><a href=#concept-document-salvageable>salvageable</a></i> state
   to false.<li><p>Let <var>ports</var> be the list of <code>MessagePort</code>s whose <span>relevant global
   object</span>'s <span>associated <code>Document</code></span> is
   <var>document</var>.<li><p>For each <var>port</var> in <var>ports</var>, <span>disentangle</span>
   <var>port</var>.<li><p>Run any <a href=#unloading-document-cleanup-steps id=destroying-documents:unloading-document-cleanup-steps>unloading document cleanup steps</a> for <var>document</var> that
   are defined by this specification and <a id=destroying-documents:other-applicable-specifications href=infrastructure.html#other-applicable-specifications>other applicable specifications</a>.<li><p>Remove any <span>tasks</span> whose <span>document</span> is <var>document</var> from any <span>task
   queue</span> (without running those tasks).<li><p>Set <var>document</var>'s <a href=document-sequences.html#concept-document-bc id=destroying-documents:concept-document-bc>browsing context</a> to
   null.<li><p>Set <var>document</var>'s <a id=destroying-documents:node-navigable href=document-sequences.html#node-navigable>node navigable</a>'s <a href=document-sequences.html#nav-active-history-entry id=destroying-documents:nav-active-history-entry>active session history entry</a>'s <a href=browsing-the-web.html#she-document-state id=destroying-documents:she-document-state>document state</a>'s <a href=browsing-the-web.html#document-state-document id=destroying-documents:document-state-document>document</a> to null.<li><p><a href=https://infra.spec.whatwg.org/#list-remove id=destroying-documents:list-remove data-x-internal=list-remove>Remove</a> <var>document</var> from the
   <span>owner set</span> of each <code id=destroying-documents:workerglobalscope><a href=workers.html#workerglobalscope>WorkerGlobalScope</a></code> object whose set <a href=https://infra.spec.whatwg.org/#list-contain id=destroying-documents:list-contains data-x-internal=list-contains>contains</a> <var>document</var>.<li><p><a href=https://infra.spec.whatwg.org/#list-iterate id=destroying-documents:list-iterate data-x-internal=list-iterate>For each</a> <var>workletGlobalScope</var> in
   <var>document</var>'s <span>worklet global
   scopes</span>, <span>terminate</span>
   <var>workletGlobalScope</var>.</ol>

  <p class=note>Even after destruction, the <code>Document</code> object itself might still be
  accessible to script, in the case where we are <a href=document-sequences.html#destroy-a-child-navigable id=destroying-documents:destroy-a-child-navigable>destroying a child navigable</a>.</p>

  <p>To <dfn id=destroy-a-document-and-its-descendants>destroy a document and its descendants</dfn> given a <code>Document</code>
  <var>document</var> and an optional set of steps <var>afterAllDestruction</var>, perform the
  following steps <span>in parallel</span>:</p>

  <ol><li>
    <p>If <var>document</var> is not <a id=destroying-documents:fully-active href=document-sequences.html#fully-active>fully active</a>, then:</p>

    <ol><li><p>Let <var>reason</var> be a string from <a href=nav-history-apis.html#ua-specific-blocking-reasons id=destroying-documents:ua-specific-blocking-reasons>user-agent specific blocking reasons</a>. If none
     apply, then let <var>reason</var> be "<code id=destroying-documents:blocking-masked><a href=nav-history-apis.html#blocking-masked>masked</a></code>".<li><p><a id=destroying-documents:make-document-unsalvageable href=browsing-the-web.html#make-document-unsalvageable>Make document unsalvageable</a> given <var>document</var> and
     <var>reason</var>.<li><p>If <var>document</var>'s <a id=destroying-documents:node-navigable-2 href=document-sequences.html#node-navigable>node navigable</a> is a
     <a id=destroying-documents:top-level-traversable href=document-sequences.html#top-level-traversable>top-level traversable</a>,
     <a id=destroying-documents:build-not-restored-reasons-for-a-top-level-traversable-and-its-descendants href=browsing-the-web.html#build-not-restored-reasons-for-a-top-level-traversable-and-its-descendants>build not restored reasons for a top-level traversable and its descendants</a>
     given <var>document</var>'s <a id=destroying-documents:node-navigable-3 href=document-sequences.html#node-navigable>node navigable</a>.</ol>
   <li><p>Let <var>childNavigables</var> be <var>document</var>'s <a href=document-sequences.html#child-navigable id=destroying-documents:child-navigable>child navigables</a>.<li><p>Let <var>numberDestroyed</var> be 0.<li>
    <p><a href=https://infra.spec.whatwg.org/#list-iterate id=destroying-documents:list-iterate-2 data-x-internal=list-iterate>For each</a> <var>childNavigable</var> of
    <var>childNavigables</var> <span class=XXX>in what order?</span>, <span>queue a
    global task</span> on the <span>navigation and traversal task source</span> given
    <var>childNavigable</var>'s <a href=document-sequences.html#nav-window id=destroying-documents:nav-window>active window</a> to perform the
    following steps:</p>

    <ol><li><p>Let <var>incrementDestroyed</var> be an algorithm step which increments
     <var>numberDestroyed</var>.<li><p><a href=#destroy-a-document-and-its-descendants id=destroying-documents:destroy-a-document-and-its-descendants>Destroy a document and its descendants</a> given <var>childNavigable</var>'s
     <a href=document-sequences.html#nav-document id=destroying-documents:nav-document>active document</a> and <var>incrementDestroyed</var>.</ol>
   <li><p>Wait until <var>numberDestroyed</var> equals <var>childNavigable</var>'s <a href=https://infra.spec.whatwg.org/#list-size id=destroying-documents:list-size data-x-internal=list-size>size</a>.<li>
    <p><span>Queue a global task</span> on the <span>navigation and traversal task source</span>
    given <var>document</var>'s <span>relevant global object</span> to perform the following
    steps:</p>

    <ol><li><p><a href=#destroy-a-document id=destroying-documents:destroy-a-document>Destroy</a> <var>document</var>.<li><p>If <var>afterAllDestruction</var> was given, then run it.</ol>
   </ol>

  <h4 id=aborting-a-document-load><span class=secno>7.5.11</span> Aborting a document load<a href=#aborting-a-document-load class=self-link></a></h4>

  <p>To <dfn id=abort-a-document>abort</dfn> a <code>Document</code> <var>document</var>:</p>

  <ol><li><p><a id=aborting-a-document-load:assert href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: this is running as part of a <span>task</span>
   queued on <var>document</var>'s <span>relevant agent</span>'s <a href=webappapis.html#concept-agent-event-loop id=aborting-a-document-load:concept-agent-event-loop>event loop</a>.<li><p>Cancel any instances of the <a href=https://fetch.spec.whatwg.org/#concept-fetch id=aborting-a-document-load:concept-fetch data-x-internal=concept-fetch>fetch</a> algorithm in the
   context of <var>document</var>, discarding any <span>tasks</span> <span>queued</span> for them, and discarding any further data received from the
   network for them. If this resulted in any instances of the <a href=https://fetch.spec.whatwg.org/#concept-fetch id=aborting-a-document-load:concept-fetch-2 data-x-internal=concept-fetch>fetch</a> algorithm being canceled or any <span>queued</span> <span>tasks</span> or any network data getting
   discarded, then <a id=aborting-a-document-load:make-document-unsalvageable href=browsing-the-web.html#make-document-unsalvageable>make document unsalvageable</a> given <var>document</var> and
   "<code id=aborting-a-document-load:blocking-fetch><a href=nav-history-apis.html#blocking-fetch>fetch</a></code>".<li>
    <p>If <var>document</var>'s <a href=dom.html#concept-document-navigation-id id=aborting-a-document-load:concept-document-navigation-id>during-loading
    navigation ID for WebDriver BiDi</a> is non-null, then:</p>

    <ol><li><p>Invoke <a id=aborting-a-document-load:webdriver-bidi-navigation-aborted href=https://w3c.github.io/webdriver-bidi/#webdriver-bidi-navigation-aborted data-x-internal=webdriver-bidi-navigation-aborted>WebDriver BiDi navigation aborted</a> with <var>document</var>'s
     <a id=aborting-a-document-load:node-navigable href=document-sequences.html#node-navigable>node navigable</a> and a new <a id=aborting-a-document-load:webdriver-bidi-navigation-status href=https://w3c.github.io/webdriver-bidi/#webdriver-bidi-navigation-status data-x-internal=webdriver-bidi-navigation-status>WebDriver BiDi navigation status</a> whose <a href=https://w3c.github.io/webdriver-bidi/#navigation-status-id id=aborting-a-document-load:navigation-status-id data-x-internal=navigation-status-id>id</a> is <var>document</var>'s <a href=dom.html#concept-document-navigation-id id=aborting-a-document-load:concept-document-navigation-id-2>during-loading navigation ID for WebDriver BiDi</a>,
     <a href=https://w3c.github.io/webdriver-bidi/#navigation-status-status id=aborting-a-document-load:navigation-status-status data-x-internal=navigation-status-status>status</a> is "<code id=aborting-a-document-load:navigation-status-canceled><a data-x-internal=navigation-status-canceled href=https://w3c.github.io/webdriver-bidi/#navigation-status-canceled>canceled</a></code>", and <a href=https://w3c.github.io/webdriver-bidi/#navigation-status-url id=aborting-a-document-load:navigation-status-url data-x-internal=navigation-status-url>url</a> is <var>document</var>'s <a href=https://dom.spec.whatwg.org/#concept-document-url id="aborting-a-document-load:the-document's-address" data-x-internal="the-document's-address">URL</a>.<li><p>Set <var>document</var>'s <a href=dom.html#concept-document-navigation-id id=aborting-a-document-load:concept-document-navigation-id-3>during-loading
     navigation ID for WebDriver BiDi</a> to null.</ol>
   <li>
    <p>If <var>document</var> has an <span>active parser</span>, then:</p>

    <ol><li><p>Set <var>document</var>'s <span>active parser was aborted</span> to true.<li><p><span>Abort that parser</span>.<li><p><a id=aborting-a-document-load:make-document-unsalvageable-2 href=browsing-the-web.html#make-document-unsalvageable>Make document unsalvageable</a> given <var>document</var> and
     "<code id=aborting-a-document-load:blocking-parser-aborted><a href=nav-history-apis.html#blocking-parser-aborted>parser-aborted</a></code>".</ol>
   </ol>

  <p>To <dfn id=abort-a-document-and-its-descendants>abort a document and its descendants</dfn> given a <code>Document</code>
  <var>document</var>:</p>

  <ol><li><p><a id=aborting-a-document-load:assert-2 href=https://infra.spec.whatwg.org/#assert data-x-internal=assert>Assert</a>: this is running as part of a <span>task</span>
   queued on <var>document</var>'s <span>relevant agent</span>'s <a href=webappapis.html#concept-agent-event-loop id=aborting-a-document-load:concept-agent-event-loop-2>event loop</a>.<li><p>Let <var>descendantNavigables</var> be <var>document</var>'s <a id=aborting-a-document-load:descendant-navigables href=document-sequences.html#descendant-navigables>descendant
   navigables</a>.<li>
    <p><a href=https://infra.spec.whatwg.org/#list-iterate id=aborting-a-document-load:list-iterate data-x-internal=list-iterate>For each</a> <var>descendantNavigable</var> of
    <var>descendantNavigables</var> <span class=XXX>in what order?</span>, <span>queue
    a global task</span> on the <span>navigation and traversal task source</span> given
    <var>descendantNavigable</var>'s <a href=document-sequences.html#nav-window id=aborting-a-document-load:nav-window>active window</a> to perform the
    following steps:</p>

    <ol><li><p><a href=#abort-a-document id=aborting-a-document-load:abort-a-document>Abort</a> <var>descendantNavigable</var>'s <a href=document-sequences.html#nav-document id=aborting-a-document-load:nav-document>active document</a>.<li><p>If <var>descendantNavigable</var>'s <a href=document-sequences.html#nav-document id=aborting-a-document-load:nav-document-2>active
     document</a>'s <i id=aborting-a-document-load:concept-document-salvageable><a href=#concept-document-salvageable>salvageable</a></i> is false, then set
     <var>document</var>'s <i id=aborting-a-document-load:concept-document-salvageable-2><a href=#concept-document-salvageable>salvageable</a></i> to
     false.</ol>
   <li><p><a href=#abort-a-document id=aborting-a-document-load:abort-a-document-2>Abort</a> <var>document</var>.</ol>

  <p id=stop-document-loading>To <dfn id=nav-stop>stop loading</dfn> a
  <a id=aborting-a-document-load:navigable href=document-sequences.html#navigable>navigable</a> <var>navigable</var>:</p>

  <ol><li><p>Let <var>document</var> be <var>navigable</var>'s <a href=document-sequences.html#nav-document id=aborting-a-document-load:nav-document-3>active
   document</a>.<li>
    <p>If <var>document</var>'s <a href=#unload-counter id=aborting-a-document-load:unload-counter>unload counter</a> is 0, and <var>navigable</var>'s
    <a id=aborting-a-document-load:ongoing-navigation href=browsing-the-web.html#ongoing-navigation>ongoing navigation</a> is a <a id=aborting-a-document-load:navigation-id href=browsing-the-web.html#navigation-id>navigation ID</a>, then <a id=aborting-a-document-load:set-the-ongoing-navigation href=browsing-the-web.html#set-the-ongoing-navigation>set the ongoing
    navigation</a> for <var>navigable</var> to null.</p>

    <p class=note>This will have the effect of aborting any ongoing navigations of
    <var>navigable</var>, since at certain points during navigation, changes to the <a id=aborting-a-document-load:ongoing-navigation-2 href=browsing-the-web.html#ongoing-navigation>ongoing
    navigation</a> will cause further work to be abandoned.</p>
   <li><p><a href=#abort-a-document-and-its-descendants id=aborting-a-document-load:abort-a-document-and-its-descendants>Abort a document and its descendants</a> given <var>document</var>.</ol>

  <p class=XXX>Through their <a href=speculative-loading.html#nav-traversal-ui>user interface</a>, user agents also
  allow stopping traversals, i.e. cases where the <a id=aborting-a-document-load:ongoing-navigation-3 href=browsing-the-web.html#ongoing-navigation>ongoing navigation</a> is "<code>traversal</code>". The above algorithm does not account for this. (On the other hand,
  user agents do not allow <code id=aborting-a-document-load:dom-window-stop><a href=nav-history-apis.html#dom-window-stop>window.stop()</a></code> to stop traversals,
  so the above algorithm is correct for that caller.) See <a href=https://github.com/whatwg/html/issues/6905>issue #6905</a>.</p>


  <nav><a href=browsing-the-web.html>← 7.4 Navigation and session
  history</a> — <a href=index.html>Table of Contents</a> — <a href=speculative-loading.html>7.6 Speculative loading →</a></nav>
